package com.coolgor.axnut.web.airuc;

import org.springframework.stereotype.Controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coolgor.airvow.Net.HttpServletRequestUtil;
import com.coolgor.axnut.dto.airuc.LessonExecution;
import com.coolgor.axnut.entity.airuc.Lesson;
import com.coolgor.axnut.enums.airuc.LessonStateEnum;
import com.coolgor.axnut.exceptions.airuc.LessonOperationException;
import com.coolgor.axnut.service.airuc.LessonService;


@Controller
@RequestMapping(value = "airuc", method = { RequestMethod.GET, RequestMethod.POST })
public class LessonManagementController {
	@Autowired
	private LessonService lessonService;
	
	@RequestMapping(value = "/getlessonbyid", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getLessonById(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Long lessonID = HttpServletRequestUtil.getLong(request, "lessonid");
		if (lessonID > -1) {
			try {
				Lesson lesson = lessonService.getLesson(lessonID);
				modelMap.put("lesson", lesson);
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty lessonId");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getLessonListByCourseID", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> getLessonListByCourseID(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
	    int page = HttpServletRequestUtil.getInt(request, "page");
	    int pageSize=HttpServletRequestUtil.getInt(request, "rows");
		Lesson lessonCondition = new Lesson();
		Long courseID = HttpServletRequestUtil.getLong(request, "courseid");
		if (null!=courseID) {
			lessonCondition.setCourseID(courseID);			
		}
		LessonExecution exe = lessonService.getLessonList(lessonCondition, page, pageSize);
		if (null != exe ) {
			try {
				modelMap.put("total",exe.getCount());
				modelMap.put("rows",exe.getLessonList());
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "Empty List");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/addLesson", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> addLesson(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Lesson lesson = new Lesson();
		//命令Tomcat使用UTF-8码表解码，而不用默认的ISO-8859-1
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		//从前端获取各个数值
		String lessonName = HttpServletRequestUtil.getString(request, "lessonName");
		if (null!=lessonName && !"".equals(lessonName)) {
			//课程名称不允许修改为空值
			lesson.setLessonName(lessonName);
		}
		String lessonDesc = HttpServletRequestUtil.getString(request, "lessonDesc");
		if (null!=lessonDesc ) {
			lesson.setLessonDesc(lessonDesc);
		}
		String teacher = HttpServletRequestUtil.getString(request, "teacher");
		if (null!=teacher) {
			lesson.setTeacher(teacher);
		}
		String teacherDesc = HttpServletRequestUtil.getString(request, "teacherDesc");
		if (null!=teacherDesc) {
			lesson.setTeacherDesc(teacherDesc);
		}		
		Long courseID = HttpServletRequestUtil.getLong(request, "courseID");
		if (null!=courseID && courseID>0) {
			lesson.setCourseID(courseID);
		} 
		String lessonPath = HttpServletRequestUtil.getString(request, "lessonPath");
		if (null!=lessonPath) {
			lesson.setLessonPath(lessonPath);
		}	
		Long priority = HttpServletRequestUtil.getLong(request, "priority");
		if (null!=priority && priority>0) {
			lesson.setPriority(priority);
		} 
		//执行更新操作
		try {
			LessonExecution exe = lessonService.addLesson(lesson);
			if (exe.getState() == LessonStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			} else {
				modelMap.put("success", false);
				modelMap.put("errMsg", exe.getStateInfo());
			}
		} catch (LessonOperationException e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.toString());
			return modelMap;
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/modifyLesson", method = RequestMethod.POST)
	@ResponseBody
//	private Map<String, Object> modifyLesson(@RequestBody List<Lesson> lessonList, HttpServletRequest request) {
	private Map<String, Object> modifyLesson(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Lesson lesson = new Lesson();
		//命令Tomcat使用UTF-8码表解码，而不用默认的ISO-8859-1了。
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		//从前端获取各个数值
		Long lessonID = HttpServletRequestUtil.getLong(request, "lessonID");
		if (null!=lessonID && lessonID>0) {
			lesson.setLessonID(lessonID);
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "ID为空，无法更新！");
			return modelMap;
		}
		String lessonName = HttpServletRequestUtil.getString(request, "lessonName");
		if (null!=lessonName && !"".equals(lessonName)) {
			//课程名称不允许修改为空值
			lesson.setLessonName(lessonName);
		}
		String lessonDesc = HttpServletRequestUtil.getString(request, "lessonDesc");
		if (null!=lessonDesc ) {
			lesson.setLessonDesc(lessonDesc);
		}
		String teacher = HttpServletRequestUtil.getString(request, "teacher");
		if (null!=teacher) {
			lesson.setTeacher(teacher);
		}
		String teacherDesc = HttpServletRequestUtil.getString(request, "teacherDesc");
		if (null!=teacherDesc) {
			lesson.setTeacherDesc(teacherDesc);
		}		
		Long courseID = HttpServletRequestUtil.getLong(request, "courseID");
		if (null!=courseID && courseID>0) {
			lesson.setCourseID(courseID);
		} 
		String lessonPath = HttpServletRequestUtil.getString(request, "lessonPath");
		if (null!=lessonPath) {
			lesson.setLessonPath(lessonPath);
		}	
		Long priority = HttpServletRequestUtil.getLong(request, "priority");
		if (null!=priority && priority>0) {
			lesson.setPriority(priority);
		} 
		//执行更新操作
		try {
			LessonExecution exe = lessonService.modifyLesson(lesson);
			if (exe.getState() == LessonStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			} else {
				modelMap.put("success", false);
				modelMap.put("errMsg", exe.getStateInfo());
			}
		} catch (LessonOperationException e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.toString());
			return modelMap;
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/removeLesson", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> removeLesson(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		//命令Tomcat使用UTF-8码表解码，而不用默认的ISO-8859-1了。
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		Lesson lesson = new Lesson();
		//从前端获取各个数值
		Long lessonID = HttpServletRequestUtil.getLong(request, "lessonID");
		if (null!=lessonID && lessonID>0) {
			lesson.setLessonID(lessonID);
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "ID为空，删除失败！");
			return modelMap;
		}
		//执行删除操作
		try {
			LessonExecution exe = lessonService.removeLesson(lesson);
			if (exe.getState() == LessonStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			} else {
				modelMap.put("success", false);
				modelMap.put("errMsg", exe.getStateInfo());
			}
		} catch (LessonOperationException e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.toString());
			return modelMap;
		}
		return modelMap;
	}
}
